package com.example.qxfw.ybzz.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.qx.YbzzForecast;
import com.example.qxfw.ybzz.mapper.YbzzForecastMapper;
import com.example.qxfw.ybzz.service.YbzzForecastService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class YbzzForecastServiceImpl implements YbzzForecastService {

    @Autowired
    private YbzzForecastMapper ybzzForecastMapper;

    @Override
    public List<String> getStationId() {
        List<String> stationids = ybzzForecastMapper.getStationId();
        return stationids;
    }

    @Override
    public List<String> getForecastTime(Integer forecastType, String userName) {
        List<String> forecastTimes = new ArrayList<>();
        List<YbzzForecast> ybzzForecasts = ybzzForecastMapper.selectList(Wrappers.<YbzzForecast>query().select("distinct forecastTime").lambda().eq(YbzzForecast::getForecasttype, forecastType).eq(YbzzForecast::getUsername, userName).orderByDesc(YbzzForecast::getForecasttime));
        for (YbzzForecast ybzzForecast : ybzzForecasts) {
            forecastTimes.add(ybzzForecast.getForecasttime());
        }
        return forecastTimes;
    }

    @Override
    public List<YbzzForecast> getDateCharAndNTimes(String forecastTimes) {
        return ybzzForecastMapper.selectList(Wrappers.<YbzzForecast>lambdaQuery().eq(YbzzForecast::getForecasttime, Timestamp.valueOf(forecastTimes)));
    }

    @Override
    public List<YbzzForecast> listByParam(String userName, String dateChar, Integer forecastType, Integer nTimes, String forecastTime) {
        List<YbzzForecast> ybzzForecasts = ybzzForecastMapper.selectList(Wrappers.<YbzzForecast>lambdaQuery()
                .eq(YbzzForecast::getUsername, userName)
                .eq(YbzzForecast::getDatechar, Timestamp.valueOf(dateChar))
                .eq(YbzzForecast::getForecasttype, forecastType)
                .eq(YbzzForecast::getNtimes, nTimes)
                .eq(YbzzForecast::getForecasttime, Timestamp.valueOf(forecastTime)));
        List<YbzzForecast> list = ybzzForecasts.stream().map(
                i -> {
                    YbzzForecast bo = new YbzzForecast();
                    BeanUtils.copyProperties(i, bo);
                    String stationId = bo.getStationid().trim();
                    String timeChar = bo.getTimechar().trim();
                    bo.setStationid(stationId);
                    bo.setTimechar(timeChar);
                    return bo;
                }
        ).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<YbzzForecast> getListByType(String forecastTime, String forecastType) {
        List<YbzzForecast> ybzzForecasts = ybzzForecastMapper.getListByType(forecastTime, forecastType);
        return ybzzForecasts;
    }

    @Override
    public Integer update(YbzzForecast ybzzForecast) {
        LambdaUpdateWrapper<YbzzForecast> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(YbzzForecast::getStationid,ybzzForecast.getStationid()).eq(YbzzForecast::getForecasttype,ybzzForecast.getForecasttype()).eq(YbzzForecast::getNtimes,ybzzForecast.getNtimes()).eq(YbzzForecast::getDatechar,ybzzForecast.getDatechar());
        int rows  = ybzzForecastMapper.update(ybzzForecast,updateWrapper);
        return rows;
    }

    @Override
    public Integer delete(String shuZhiDate, String forecastType, String strTimeChar, String stationList) {
        int rows = ybzzForecastMapper.delete(shuZhiDate,forecastType,strTimeChar,stationList);
        return rows;
    }

    @Override
    public Integer save(YbzzForecast ybzzForecast) {
        int rows = ybzzForecastMapper.insert(ybzzForecast);
        return rows;
    }
}
